<?
require_once($r."_config/sp.php");
require_once($r."_config/email.php");
require_once($r."_engine/types.php");
require_once($r."_engine/mail_functions.php");


define("PACKET_TYPE_DIFF",			0x00000001);
define("PACKET_TYPE_CREATE_EMAIL",	0x00000002);
define("PACKET_TYPE_CREATE_FEED",	0x00000003);

define("PACKET_SUBTYPE_REQUEST",	0x00000001);
define("PACKET_SUBTYPE_ANSWER",		0x00000002);

define("OPERATION_STATUS_SUCCESSFUL",			0x00000000);
define("OPERATION_STATUS_NOT_FOUND",			0x00000001);
define("OPERATION_STATUS_DB_ERROR",				0x00000002);
define("OPERATION_STATUS_EMAIL_CREATION_ERROR",	0x00000003);


class CSPGate
{
	function SendReqAndGetResponse($req)
	{
		global $g_sp_gate_server_name, $g_sp_gate_server_port, $g_sp_gate_timeout;

		// Open socket
		$fp = @fsockopen($g_sp_gate_server_name, $g_sp_gate_server_port, 
			$errno, $errstr, $g_sp_gate_timeout);
		if (!$fp) {
			report_error(
				"W3M Service",
				"Could not connect to the service. Params:\n".
				"Server name: ".$g_sp_gate_server_name."\n".
				"Server port: ".$g_sp_gate_server_port."\n".
				"Error code:   ".$errno."\n".
				"Error msg:    ".$errstr."\n"
			);
			return false;
		}
		
		// Send request
		@fputs($fp, $req);
		
		// Get response
		$resp = "";
		while (!feof($fp))
			$resp .= fgets($fp, 50000);

		fclose($fp);
		return $resp;
	}
	
	function PrintBuffer($buffer)
	{
		$l = strlen($buffer);
		echo "<pre>\n";
		echo "BUFFER ($l bytes):\n";
		//if ($l > 100) $l = 100;
	
		for ($i=0; $i < $l; $i++) 
		{
			echo bin2hex($buffer[$i])." ";
		}
	
		echo "</pre>\n";
	}

/* ------------------------------------------------------------- */
/* ---------              GET DIFF HIGHLIGHT           --------- */
/* ------------------------------------------------------------- */

	function GetDiffHighLight($monitor_id, $resource1_id, $resource2_id,
		$highlight_font_color, $highlight_bk_color, $highlight_font_style,
		$dhl_type)
	{
		global $g_sp_gate_dabug, $g_site_config;

		if ($g_site_config == "kvv_home" && !$g_sp_gate_dabug)
		{
			// TODO: replace above condition with one global option
			$d["diff_result"] = '<div align="center"><h1>Test message</h1></div>';
			return $d;
		}
		
		$b = "";
		$b .= pack("V", PACKET_TYPE_DIFF);
		$b .= pack("V", PACKET_SUBTYPE_REQUEST);
		$b .= pack("V", $monitor_id);	
		$b .= pack("V", $resource1_id);
		$b .= pack("V", $resource2_id);
		$b .= pack("V", $highlight_font_color);
		$b .= pack("V", $highlight_bk_color);
		$b .= pack("V", $highlight_font_style); 
		$b .= pack("V", $dhl_type); 

		
		if ($g_sp_gate_dabug) $this->PrintBuffer($b);

		$b = $this->SendReqAndGetResponse($b);

		if ($g_sp_gate_dabug) $this->PrintBuffer($b);


		$d = @unpack(
			"V1"."type"."/".
			"V1"."subtype"."/".
			"V1"."operation_status"."/".
			"V1"."triggers_count"."/".
			"V1"."result_length",
			$b);

		$d["diff_result"] = substr($b, 4+4+4+4+4, $d["result_length"]-1);

		if ($g_sp_gate_dabug) 
		{
			echo "<pre>";
			var_dump($d);
			echo "</pre>";
		}
		
		return $d;
	}

	function GetDiffHighLight_OLD($monitor_id, $resource1_id, $resource2_id)
	{
		global $g_sp_gate_dabug;
		
		$b = "";
		$b .= pack("V", PACKET_TYPE_DIFF);
		$b .= pack("V", PACKET_SUBTYPE_REQUEST);
		$b .= pack("V", $monitor_id);	
		$b .= pack("V", $resource1_id);
		$b .= pack("V", $resource2_id);
		
		if ($g_sp_gate_dabug) $this->PrintBuffer($b);

		$b = $this->SendReqAndGetResponse($b);

		if ($g_sp_gate_dabug) $this->PrintBuffer($b);


		$d = unpack(
			"V1"."type"."/".
			"V1"."subtype"."/".
			"V1"."operation_status"."/".
			"V1"."triggers_count"."/".
			"V1"."result_length",
			$b);

		$d["diff_result"] = substr($b, 4+4+4+4+4, $d["result_length"]-1);

		if ($g_sp_gate_dabug) 
		{
			echo "<pre>";
			var_dump($d);
			echo "</pre>";
		}
		
		return $d;
	}
	

/* ------------------------------------------------------------- */
/* ---------              REQUEST FEED                 --------- */
/* ------------------------------------------------------------- */

	function RequestFeed($monitor_id, $feed_type)
	{
		/*
		feed_type => 0x00000001 - RSS, 0x000000002 - ATOM, 0x000000003 - RSS | ATOM
		*/
		global $g_sp_gate_dabug;
		
		$b = "";
		$b .= pack("V", PACKET_TYPE_CREATE_FEED);
		$b .= pack("V", PACKET_SUBTYPE_REQUEST);
		$b .= pack("V", $monitor_id);	
		$b .= pack("V", $feed_type);
		
		if ($g_sp_gate_dabug) $this->PrintBuffer($b);

		$b = $this->SendReqAndGetResponse($b);

		if ($g_sp_gate_dabug) $this->PrintBuffer($b);

		$d = unpack(
			"V1"."operation_status",
			$b);

		if ($g_sp_gate_dabug) 
		{
			echo "<pre>";
			var_dump($d);
			echo "</pre>";
		}
		
		return $d;
	}

/* ------------------------------------------------------------- */
/* ---------               SEND  E-MAIL                --------- */
/* ------------------------------------------------------------- */

	function PackStringToBuffer($str)
	{
		$b = "";
		$b .= pack("V", strlen($str)+1);
		$b .= $str;
		$b .= "\x00";
		return $b;
	}
	
	function PackVariablesToBuffer($list)
	{
		$b = "";
		foreach($list as $k => $v)
		{
			if (strlen($b) > 0) $b .= "\x00";
			$b .= "[%" . $k . "%]". "\x00" . $v;
		}
		return $this->PackStringToBuffer($b);
	}

	function GetParam(&$params, $name, $default)
	{
		if (isset($params[$name])) return $params[$name];
		return $default;
	}

	function SendEmail($params)
	{
		global $g_sp_gate_dabug, $g_site_config;
		// TODO: make as option
		if ($g_site_config == "kvv_home" || $g_site_config == "kvv_mobile") return true;
		
		if ($g_sp_gate_dabug)
		{
			echo "<pre>";
			htmlentities(var_dump($params));
			echo "</pre>";
		}

		$b2 = "";
		$b2 .= pack("V", 
			$this->GetParam($params, "account_id", 0));
		$b2 .= pack("V", 
			$this->GetParam($params, "monitor_id", 0));
		$b2 .= pack("C", 
			$this->GetParam($params, "email_content_type", ALERT_FORMAT_PLAIN_TEXT));
		$b2 .= pack("V", 
			$this->GetParam($params, "alert_at_time", -1));
		$b2 .= $this->PackStringToBuffer(
			$this->GetParam($params, "email_from", ""));
		$b2 .= $this->PackStringToBuffer(
			$this->GetParam($params, "email_to", ""));
		$b2 .= $this->PackStringToBuffer(
			$this->GetParam($params, "email_cc", ""));
		$b2 .= $this->PackStringToBuffer(
			$this->GetParam($params, "email_bcc", ""));
		$b2 .= $this->PackVariablesToBuffer(
			$this->GetParam($params, "variables", array()));
		$b2 .= $this->PackStringToBuffer(
			$this->GetParam($params, "email_template_name", ""));
		$b2 .= $this->PackStringToBuffer(
			$this->GetParam($params, "email_subject", ""));
		$b2 .= $this->PackStringToBuffer(
			$this->GetParam($params, "email_template", ""));
		
		$b = "";
		$b .= pack("V", PACKET_TYPE_CREATE_EMAIL);
		$b .= pack("V", PACKET_SUBTYPE_REQUEST);
		$b .= pack("V", strlen($b2) + 4); // total_size
		$b .= $b2;
		
	
		if ($g_sp_gate_dabug) $this->PrintBuffer($b);

		$b = $this->SendReqAndGetResponse($b);

		if ($g_sp_gate_dabug) $this->PrintBuffer($b);

		$d = unpack(
			"V1"."type"."/".
			"V1"."subtype"."/".
			"V1"."operation_status",
			$b);

		if ($g_sp_gate_dabug) 
		{
			echo "<pre>";
			var_dump($d);
			echo "</pre>";
		}
		
		if ($d["operation_status"] != 0) 
		{
			die("Server error. Please report about this probem to webmaster or support.");
		}

		return $d;
	}
	
}


/* ------------------------------------------------------------- */
/* --------      Global instance for quick using       --------- */
/* ------------------------------------------------------------- */

$g_spg = new CSPGate();

?>